const puppeteer = require('puppeteer');
const account = require('./account');
const timeout = 3000 * 1000;
const homePage = `https://login.taobao.com/member/login.jhtml?tpl_redirect_url=https%3A%2F%2Fwww.tmall.com&style=miniall&enup=true&newMini2=true&full_redirect=true&sub=true&from=tmall&allp=assets_css%3D3.0.10/login_pc.css&pms=1584337264259%22`;
const verification =  require('./slider');
const userAgent = require('../../tools/userAgent').random;
const randomSec = (()=>{
    return 100+1900*Math.random;
});

//const devices = require('puppeteer/DeviceDescriptors');
//const iphone = devices['iPhone 6']
const pConf = { 
    width: 1242, height: 2208,
    headless: false,
    //slowMo: 30,
    ignoreDefaultArgs:['--enable-automation'],
    executablePath:'C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe'
}


let login = (async ()=>{
    let browser = await (puppeteer.launch(pConf));//headless:false 时会打开浏览器界面
    let page = await browser.newPage();
    let ua = userAgent();
    await page.setUserAgent(ua);
    await page.evaluateOnNewDocument(()=>{
        const newProto = navigator.__proto__;
        delete newProto.webdriver;
        navigator.__proto__ = newProto;
    });
    //await page.emulate(iphone);
    //设置可视区域大小
    await page.setViewport({ width: 1280, height: 1024 });
    await page.goto(homePage, { 'timeout': timeout });
    await page.waitForSelector('#TPL_username_1',{ 'timeout': timeout });
    await page.waitForSelector('#TPL_password_1',{ 'timeout': timeout });
    await page.waitForSelector('#J_SubmitStatic',{ 'timeout': timeout });
    await page.waitFor(randomSec());
    let static = await page.$('.module-static');
    console.log(static);
    //await page.waitFor(600000);
    if(!static){
        let _switch  = await page.$('.login-switch');
        await _switch.click();
        await page.waitFor(randomSec());
    }
    //执行js脚本，写入账号信息 evaluate传参
    /*
    await page.evaluate((account) => {
        // if(!document.querySelector('.module-static')){
        //     document.querySelector('.login-switch').click();
        // }
        let username = document.querySelector('#TPL_username_1');
        let password = document.querySelector('#TPL_password_1');
        username.value = 'account.account';
        password.value = account.password;
    },account);
    */
    let username = await page.$('#TPL_username_1');
    console.log(1);
    await username.type(account.username); 
    await page.waitFor(2000);
    //let password = await page.$('#TPL_password_1');
    //await password.type(account.password);
    await typePasswordAndSubmit(page);
    await page.waitForNavigation({ 'timeout': timeout });
    await page.waitFor(10000);
    let again = await page.evaluate(()=>{
        return document.querySelector('#TPL_username_1')!=null;
    });
    if(again){
        console.log('again');
        await typePasswordAndSubmit(page);
        await page.waitForNavigation({'timeout':timeout});
        //await page.waitFor(10000);
    }
    console.log('cookie');
    let cookie = await page.evaluate(() => {
        let loginSuccess = false;
        if(document.querySelector("#mallPage")){
            loginSuccess = true;
        }
        return {
            'login':loginSuccess,
            'value':document.cookie
        };
    });
    console.log(cookie);
});

let typePasswordAndSubmit = ((page)=>{
    return new Promise(async (resolve,reject)=>{
        await page.evaluate((account) => {
            // if(!document.querySelector('.module-static')){
            //     document.querySelector('.login-switch').click();
            // }
           // let username = document.querySelector('#TPL_username_1');
            let password = document.querySelector('#TPL_password_1');
            //username.value = account.username;
            password.value = account.password;
        },account);
        console.log(2);
        await page.waitFor(randomSec());
        let btn = await page.$('#J_SubmitStatic');
        //await verification(page);//解决滑块验证
        console.log(3);
        await page.waitFor(2000);
        await btn.click();
        console.log(4);
        resolve("fin");
    });
})

login();
